﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

namespace Bouyei.GeoCore.GeoParsers.Parser
{
    using Dbf;
    /// <summary>
    /// 摘自网上开源收集整理,*.dbf文件解析
    /// </summary>
    public class DbfParser
    {
        private Encoding encoding = Encoding.UTF8;
        public DbfParser(Encoding encoding)
        {
            this.encoding = encoding;
        }

        // Read an entire standard DBF file into a DataTable
        public DataTable FromReader(string dbfFile)
        {
            using (DbfReader reader = new DbfReader(dbfFile, encoding))
            {
                return reader.Reader();
            }
        }
        public void FromStreamReader(string dbfFile, Func<DataColumn[], List<object>, bool> fun)
        {
            using (DbfReader reader = new DbfReader(dbfFile, encoding))
            {
                reader.StreamReader(fun);
            }
        }
        public List<DataColumn> FromReaderColumns(string dbfFile)
        {
            using (DbfReader reader = new DbfReader(dbfFile, encoding))
            {
                return reader.ReadColumns();
            }
        }
        public int ToWriter(string dbfFile, DataTable data)
        {
            if (encoding == null) encoding = Encoding.UTF8;

            using (DbfWriter writer = new DbfWriter(dbfFile, encoding))
            {
                return writer.Writer(data);
            }
        }
        public int ToWriter(string dbfFile, DbfColumn[] columns, List<object[]> rows)
        {
            if (encoding == null) encoding = Encoding.UTF8;

            using (DbfWriter writer = new DbfWriter(dbfFile, encoding))
            {
                return writer.Writer(columns, rows);
            }
        }
        public int ToWriter<T>(string dbfFile, List<T> list)
        {
            if (encoding == null) encoding = Encoding.UTF8;

            using (DbfWriter writer = new DbfWriter(dbfFile, encoding))
            {
                return writer.Writer<T>(list);
            }
        }

    }
}
